﻿<%@ Page Language="C#" Inherits="Dimok.Web.UI.PageToPDF" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
	public override string FileName { get { return Page.Title+"_" + LoanID; } }
	int LoanID { get { return int.Parse("0" + Request["ApplID"]); } }
	string LoanIDs { get { return Request["LoanIDs"] + ""; } }
	int CompanyID { get { return int.Parse("0" + Request["CompanyID"]); } }
	string filter = null;
	string Filter { 
		get {
			if (filter != null) return filter;
			if (Cache[Request["Filter"] + ""] != null) return filter = Cache[Request["Filter"] + ""] + "";
			if (Request["Filter"] != "") return filter = Request["Filter"] + "";
			if (LoanID > 0) return filter = "LoanID=" + LoanID;
			return filter = ""; 
		}
	}
	string Letter { get { return Request["Letter"] + ""; } }
	int LettersCount = 0;
	public bool UserCredentials { get { return LettersCount < 2; } }
	protected override void OnInit(EventArgs e) {
		base.OnInit(e);
		rptLetters.ItemCreated += new RepeaterItemEventHandler(rptLetters_ItemCreated);
		if (Filter == "") throw new Exception("Must select loans for letters.");
		dsExport.FilterDefault = Filter;
		dsExport.Selected += new ObjectDataSourceStatusEventHandler(dsExport_Selected);
	}
	void dsExport_Selected(object sender, ObjectDataSourceStatusEventArgs e) {
		LettersCount = ((System.Data.DataView)e.ReturnValue).Count;
	}

	public int LoanID_DataBind = 0;
	void rptLetters_ItemCreated(object sender, RepeaterItemEventArgs e) {
		DataRowView row = (DataRowView)e.Item.DataItem;
		if (row["LoanID"] == DBNull.Value) {
			Response.Clear();
			Response.Write("<pre>Filter:"+Filter+"<br>Account " + row["UsixID"] + " does not have a LoanID.</pre>");
			Response.End();
		}
		LoanID_DataBind = (int)row["LoanID"];
		object c = DrawLetter((int)row["LoanID"], Convert.ToInt32(row["CompanyID"]));
		if (c != null) e.Item.Controls.AddAt(0, c as Control);
		else e.Item.Controls.Clear();
	}
	Control DrawLetter(int LoanID,int CompanyID){
		string controlPath1 = ConfigurationSettings.AppSettings[Letter + ".Path"];
		var cID = new USIXExportDataSource().SelectEx(LoanID)[0]["CompanyID"] + "";
		string controlPath2 = ConfigurationSettings.AppSettings[Letter + "." + cID + ".Path"];
		string controlPath3 = ConfigurationSettings.AppSettings[Letter + ".Name"];// +"_XXX";
		try {
			//throw new Exception("controlPath:" + controlPath);
			Control c = LoadControl(controlPath1+controlPath2+controlPath3);
			if( LoanID>0)
				Lib.SetProperty(c, "LoanID", LoanID);
			else
				Lib.SetProperty(c, "Filter", Filter);
			//System.Reflection.PropertyInfo pi = c.GetType().GetProperty("LoanID");
			//if (pi == null) throw new NotImplementedException("LoanID property is not implemented in " + controlPath);
			//pi.SetValue(c, LoanID,new object[]{});
			//bool wasLetterSent = Data.SQL.ExecuteSP("s_COllection_LetterSentNote", LoanID, Letter) == 0;
			bool wasLetterSent = new MotoBank().s_COllection_LetterSentNote(LoanID, Letter) == 0;
			if (LettersCount > 1 && wasLetterSent) return null;
			return c;
		} catch (Exception exc) {
			exc.Data.Add("controlPath1", controlPath1);
			exc.Data.Add("controlPath2", controlPath2);
			exc.Data.Add("controlPath3", controlPath3);
			exc.Data.Add("cID", cID);
			exc.Data.Add("LoanID", LoanID);
			throw new Dimok.Exceptions.ASPException(exc);
		}
	}
	protected override void OnLoad(EventArgs e) {
		base.OnLoad(e);
	}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head RunAt="server">
<style>
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	margin-bottom:.0001pt;
	text-align:justify;
	font-size:12.0pt;
	font-family:"Times New Roman","serif";}
h1{	font-size:16.0pt;}
h1,h2,h3,h4,h5,h6
{
	margin:0in;
	margin-bottom:.0001pt;
	text-align:center;
	page-break-after:avoid;
	font-family:"Century","serif";}
h5
{
	font-size:14.0pt;
	font-family:"Times New Roman","serif";
	font-weight:normal;}
h6
{
	font-size:12.0pt;
	font-family:"Times New Roman","serif";}
p.MsoTitle, li.MsoTitle, div.MsoTitle
	{margin:0in;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:20.0pt;
	font-family:"Century","serif";
	font-weight:bold;}
p.MsoBodyText, li.MsoBodyText, div.MsoBodyText
	{margin:0in;
	margin-bottom:.0001pt;
	text-align:justify;
	font-size:14.0pt;
	font-family:"Times New Roman","serif";}
p.MsoSubtitle, li.MsoSubtitle, div.MsoSubtitle
	{margin:0in;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:16.0pt;
	font-family:"Century","serif";
	font-weight:bold;}
@page Section1
{size:8.5in 11.0in;
margin: 1in 1in 1in 1in;
border:double windowtext 4.5pt;
padding:24.0pt 24.0pt 24.0pt 24.0pt;}
div.Section1{
width:85%;
page:Section1;}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:Repeater ID="rptLetters" DataSourceID="dsExport" runat="server"><ItemTemplate>
<div style="page-break-after:always" runat="server">&nbsp;</div>
</ItemTemplate></asp:Repeater>
</form>
<DS:USIXExportDataSource ID="dsExport" EmtyRowPolicy="None" runat="server">
</DS:USIXExportDataSource>
</body>
</html>
